<?php


namespace app\controller\admin;

use app\model\AdminClass;
use app\model\AdminStaff;
use app\util\ReturnCode;
use think\facade\Request;
use think\facade\Validate;
use think\Response;

class Class2 extends Base
{
    public function index() : Response {
        $limit = $this->request->get('size', config('apiadmin.ADMIN_LIST_DEFAULT'));
        $start = $this->request->get('page', 1);
        $school_id = $this->request->get('school_id', $this->userInfo['school_id']);
        $name = $this->request->get('name', '');
        $sortKey = $this->request->get('sortKey', 'create_time');
        $order = $this->request->get('order', 'DESC');

        $where = [
            ['AdminClass.is_delete', '=', 0]
        ];
        $obj = (new AdminClass())->alias('AdminClass');
        if ($school_id > 0) {
            array_push($where, ['AdminClass.school_id', '=', $school_id]); // 查询报错
        }
        if ($name) {
            array_push($where, ['AdminClass.class', 'like', "%{$name}%"]);
        }
        $listObj = $obj->where($where)->with(['school', 'staff'])
            ->order('AdminClass.'.$sortKey, $order)
            ->paginate(['page' => $start, 'list_rows' => $limit])->toArray();

        return $this->buildSuccess([
            'list'  => $listObj['data'],
            'count' => $listObj['total']
        ]);
    }

    public function add(): Response {
        $school_id = Request::post('school_id/d');
        $staff_id = $this->request->post('staff_id', '');
        $name = $this->request->post('name', '');
        $notes = $this->request->post('notes', '');
        $validate = Validate::rule([
            'school_id|学校'=>'require',
            'staff_id|班主任'=>'require',
            'name|班级' => 'require',
        ]);
        if (!$validate->check(input('post.'))) {
            return $this->buildFailed(ReturnCode::PARAM_INVALID, $validate->getError());
        }
        $classCount = (new AdminClass())->where([
            'is_delete' => 0,
            'school_id' => $school_id,
            'name' => $name
        ])->count();
        if ($classCount > 0) {
            return $this->buildFailed(ReturnCode::DATA_EXISTS, '班级名称已被使用');
        }
        $adminClass = new AdminClass();
        $adminClass->school_id = $school_id;
        $adminClass->staff_id = $staff_id;
        $adminClass->name = $name;
        $adminClass->notes = $notes;
        if (!$adminClass->save()) {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR);
        }
        return $this->buildSuccess();
    }

    public function edit(): Response {
        $adminClass = (new AdminClass())->where([
            'is_delete' => 0,
            'id' => $this->request->post('id')
        ])->find();
        if (!$adminClass) {
            return $this->buildFailed(ReturnCode::RECORD_NOT_FOUND, '班级未找到');
        }
        $school_id = Request::post('school_id/d');
        $staff_id = $this->request->post('staff_id', '');
        $name = $this->request->post('name', '');
        $notes = $this->request->post('notes', '');
        $classCount = (new AdminClass())->where([
            'is_delete' => 0,
            'school_id' => $school_id,
            'name' => $name
        ])->where('id' ,'<>', $adminClass->id)->count();
        if ($classCount > 0) {
            return $this->buildFailed(ReturnCode::DATA_EXISTS, '班级名称已被使用');
        }
        $adminClass->school_id = $school_id;
        $adminClass->staff_id = $staff_id;
        $adminClass->name = $name;
        $adminClass->notes = $notes;
        if (!$adminClass->save()) {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR);
        }
        return $this->buildSuccess();
    }

    public function del(): Response {
        $adminClass = (new AdminClass())->where([
            'is_delete' => 0,
            'id' => $this->request->get('id')
        ])->find();
        if (!$adminClass) {
            return $this->buildFailed(ReturnCode::RECORD_NOT_FOUND, '班级未找到');
        }
        $adminClass->is_delete = 1;
        $adminClass->save();
        return $this->buildSuccess();
    }

}